package com.xyxe.domain.strategy.model.entity;

import com.xyxe.types.common.Constants;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.*;
import java.util.stream.Collectors;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class StrategyRuleEntity {
    private Long strategyId;
    private Integer awardId;
    private Integer ruleType;
    private String ruleModel;
    private String ruleValue;
    private String ruleDesc;

    /**
     * 获取权重值 4000:102,103,104 5000:102,103,104,105
     * @return
     */
    public Map<String, List<Integer>> getRuleWeightValues() {
        if (!"rule_weight".equals(ruleModel)) return null;
        Map<String, List<Integer>> resultMap = new HashMap<>();
        String[] ruleValueGroup = ruleValue.split(Constants.SPACE);

        for (String ruleValue : ruleValueGroup) {
            //检查输入是否为空
            if (ruleValue == null || ruleValue.isEmpty()) return resultMap;

            //分割字符串获取键和值
            String[] splitRuleValue = ruleValue.split(Constants.COLON);
            if (splitRuleValue.length != 2) {
                throw new IllegalArgumentException("Rule value is not correct: " + ruleValue);
            }
            //解析值
            String[] splitRuleDetail = splitRuleValue[1].split(Constants.SPLIT);
            ArrayList<Integer> values = Arrays.stream(splitRuleDetail)
                    .map(Integer::valueOf)
                    .collect(Collectors.toCollection(ArrayList::new));
            resultMap.put(ruleValue, values);
        }
        return resultMap;
    }
}
